package org.hgvc.webapp.ets.jsf.beans.mastersetup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Nationality;
import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class NationalityBean extends AbstractBean {
	public static final String BEAN_NAME="nationalityBean";
	
	private MasterSetupService service;
	private Nationality nationality;
	private List<Nationality> nationalityList;

	private boolean catchError;
	
	public Nationality getNationality() {
		return nationality;
	}
	public void setNationality(Nationality nationality) {
		this.nationality = nationality;
	}
	public List<Nationality> getNationalityList() {
		return nationalityList;
	}
	public void setService(MasterSetupService service) {
		this.service = service;
	}

	public String initListAction(){
		return navCommonAction(false);
	}

	public void initListActionEvent(ActionEvent event){
		try{
			nationalityList = service.getNationalityList();
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
	
	public String navCommonAction(boolean detectCacheError){
		String outcome = AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (detectCacheError){
			if (this.catchError)
				outcome = null;
			this.catchError=false;
		}
		return outcome;
	}
	
	public String navAddAction(){
		return navCommonAction(true);
	}
	public void navAddActionEvent(ActionEvent event){
		this.catchError=false;
		this.nationality=new Nationality();
	}
	public String addAction(){
		return navCommonAction(true);
	}
	public void addActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.nationality.setCreatedBy(getThisUser());
			this.nationality.setCreatedDate(new Date());
			this.nationality.setIsDeleted(CommonConstant.NOT_DELETED);
			this.service.saveNationality(this.nationality);
			this.nationalityList=this.service.getNationalityList();
		}catch(BaseException be){
			initErrorBean(be);
			this.catchError=true;
		}
	}
	
	public String navEditAction(){
		return navCommonAction(false);
	}
	public void navEditActionEvent(ActionEvent event){
		UIParameter param = (UIParameter)event.getComponent().findComponent("nationalityId");
		String nationalityId = param.getValue().toString();
		
		for (Nationality n : this.nationalityList){
			if (n.getNationalityId().equalsIgnoreCase(nationalityId)){
				this.nationality = n;
				break;
			}
		}
	}
	public String updateAction(){
		return navCommonAction(true);
	}
	public void updateActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.nationality.setModifiedBy(getThisUser());
			this.nationality.setModifiedDate(new Date());
			this.service.updateNationality(this.nationality);
			this.nationalityList=this.service.getNationalityList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String deleteAction(){
		return navCommonAction(true);
	}
	public void deleteActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			List<Nationality> deleteList = new ArrayList<Nationality>();
			
			for (Nationality n : this.nationalityList){
				if (n.isMarkAsSelect())
					deleteList.add(n);
			}
			
			this.service.deleteNationality(deleteList);
			this.nationalityList=this.service.getNationalityList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
}
